package java_08;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

public class java_06 {
	public static void main(String[] args) throws Exception {
		judge();
		List<String> Call = ReadName();
		List<String> names = new ArrayList<>(Call);
		Random random = new Random();
		int index = random.nextInt(names.size());
		String selectedName = names.get(index);
		System.out.println(selectedName);
		names.remove(index);
		BufferedWriter bw = new BufferedWriter(new FileWriter("Callname1.txt"));
		for (String list : names) {
			bw.write(list);
			bw.newLine();
		}
		bw.close();
	}
	public static void judge() throws IOException {
		File file = new File("Callname1.txt");
		if (( file.length() == 0)) {
			SelectName();
		}
	}
	public static List<String> ReadName() throws IOException {
		BufferedReader b1 = new BufferedReader(new FileReader("Callname1.txt"));
		List<String> lines=b1.lines().collect(Collectors.toList());
		b1.close();
		return lines;
	}
	public static void SelectName() throws IOException {
		BufferedReader bf = new BufferedReader(new FileReader("Names.txt"));
		ArrayList<String> infor = new ArrayList<>();
		String arr;
		while ((arr = bf.readLine())!=null) {
			infor.add(arr);
		}
		bf.close();
		Collections.shuffle(infor);
		List<String> List = infor.stream().limit(10)
				.collect(Collectors.toList());
		List<String> listAll = new ArrayList<>(List);
		Collections.shuffle(listAll);  
		File f1 = new File("Callname1.txt");
		BufferedWriter out = new BufferedWriter(new FileWriter(f1));
		for (String list : listAll) {
			out.write(list);
			out.newLine();
		}
		out.close();
	}
}
